#include "usart.h"

extern USARTBuffer bluetoothInfo;
extern uint8_t AtRxBuffer[512];
extern uint8_t Rxcouter;

// Test USART Port
USARTBuffer usart_buffer;              // USART Buffer
GPIO_InitTypeDef GPIO_InitStructure;   // GPIO Struct(GPIO结构体)
USART_InitTypeDef USART_InitStructure; // USART Struct(串口结构体)
NVIC_InitTypeDef NVIC_InitStructure;   // NVIC Struct(中断结构体)

// Function of Sending data Byte
void USART_SendByte(USART_TypeDef *usart, uint8_t DataByte)
{
    USART_SendData(usart, DataByte);
    while (USART_GetFlagStatus(usart, USART_FLAG_TXE) == RESET)
        ;
}

// Function of Sending data Array
void USART_SendArray(USART_TypeDef *usart, uint8_t *Array, uint8_t Length)
{
    for (uint16_t index = 0; index < Length; index++)
    {
        USART_SendByte(usart, Array[index]);
    }
}

// Function of Sending data String
void USART_SendString(USART_TypeDef *usart, char *String)
{
    uint16_t index = 0;
    while (String[index] != '\0')
    {
        USART_SendByte(usart, String[index]);
        index += 1;
    }
}

// Function of Pow in USART Serial
uint32_t USART_Pow(uint32_t x, uint32_t pow)
{
    uint32_t result = 1;
    while (pow--)
        result *= x;
    return result;
}

// Function of Sending Number
void USART_SendNumber(USART_TypeDef *usart, uint32_t Number, uint8_t Length)
{
    for (uint8_t i = 0; i < Length; i++)
    {
        USART_SendByte(usart, Number / USART_Pow(10, Length - i - 1) % 10 + '0'); // Send data
    }
}

// Get USART Port(获取USART端口)
USART_TypeDef *getUSARTPortTypeDef(uint16_t port_tx, uint16_t port_rx, GPIO_TypeDef *gpio_pin)
{
    return USART1;
}

// Function of printf
void USART_Printf(USART_TypeDef *usart, char *format, ...)
{
    char string[100];
    va_list args;
    va_start(args, format);
    vprintf(format, args);
    va_end(args);
    USART_SendString(usart, string);
}

// Clear USART Buffer(清空USART缓冲区)
void clearUSARTBuffer(USART_TypeDef *usart)
{
    unsigned int i = 0, c;
    c = USART_RX_Buffer_Length;
    USART_ITConfig(usart, USART_IT_RXNE, DISABLE);
    if (usart == USART1)
    {
        for (i = 0; i < c; i++)
        {
            bluetoothInfo.USART_RX_Buffer[i] = 0;
        }
        bluetoothInfo.USART_RX_Index = 0;
    }
    if (usart == USART2)
    {
        for (i = 0; i < c; i++)
        {
            usart_buffer.USART_RX_Buffer[i] = 0;
        }
        usart_buffer.USART_RX_Index = 0;
    }
    USART_ITConfig(usart, USART_IT_RXNE, ENABLE);
}

void TestUASRT_Init(uint32_t baudRate)
{
    // Enable USART1 clock(使能蓝牙串口1和时钟)
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);

    // Configure USART1 pins(配置蓝牙串口1引脚)
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; // PINA2
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOA, &GPIO_InitStructure);
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; // PINA3
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOA, &GPIO_InitStructure);

    // Configure parameters of USART1(配置串口1参数)
    USART_InitStructure.USART_BaudRate = baudRate;
    USART_InitStructure.USART_WordLength = USART_WordLength_8b;
    USART_InitStructure.USART_StopBits = USART_StopBits_1;
    USART_InitStructure.USART_Parity = USART_Parity_No;
    USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
    USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
    USART_Init(USART2, &USART_InitStructure);

    // NVIC Configuration(中断配置)
    USART_ClearFlag(USART2, USART_FLAG_TC);
    USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);  // Open NVIC of USART1(打开蓝牙串口1的中断)
    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); // Set NVIC priority(设置NVIC分组)

    NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);

    usart_buffer.USART_TX_Index = 0;
    usart_buffer.USART_RX_Index = 0;
    usart_buffer.USART_TX_Complete = 0;
    usart_buffer.USART_RX_Complete = 0;
    usart_buffer.USART_TXFlag = 0;
    usart_buffer.USART_RXFlag = 0;
    usart_buffer.USART_TX_DataSlice = 0;
    usart_buffer.USART_RX_DataSlice = 0;

    USART_Cmd(USART2, ENABLE); // Enable USART2(使能蓝牙串口2)
}

void USART2_IRQHandler(void)
{
    if (USART_GetITStatus(USART2, USART_IT_RXNE) == SET)
    {
        // Read data from USART1(从串口2读取数据)
        uint8_t Bluetooth_ReceiveData = USART_ReceiveData(USART2);
        // Send data to buffer(将数据存到缓冲区)
        usart_buffer.USART_RX_Buffer[usart_buffer.USART_RX_Index] = Bluetooth_ReceiveData;
        usart_buffer.USART_RX_Buffer_Slice[usart_buffer.USART_RX_Index] = Bluetooth_ReceiveData;
        usart_buffer.USART_RX_Index++;
        Rxcouter++;
        usart_buffer.USART_RX_DataSlice = Bluetooth_ReceiveData;
        // If buffer is full, clear it(如果缓冲区满了，清空缓冲区)
        if (Bluetooth_ReceiveData == USART_RX_End_Char || usart_buffer.USART_RX_Index >= (uint8_t)(USART_RX_Buffer_Length))
        {
            usart_buffer.USART_RX_Index = 0;
            usart_buffer.USART_RX_Complete = 1;
            memcpy(AtRxBuffer, usart_buffer.USART_RX_Buffer, usart_buffer.USART_RX_Index);
        }
        else
        {
            usart_buffer.USART_RXFlag = 1;
        }
        // Clear USART Buffer(清除串口2的缓冲区)
        USART_ClearITPendingBit(USART2, USART_IT_RXNE);
    }
}
